<?php

/**
 * Description for Form Model
 *
 * @author Pavel Savchuk
 */
class Backend_FormModel extends RCMS_Core_BaseModel {

    private $_pageTableName = 'page';
    private $_variableTableName = 'variable_content';
    private $_configTableName = 'config';
    private $_formsTableName = 'forms';
	//@todo move it from here!
	public function selectConfigForImage() {
        $select = $this->_adapter->select('name, value')->from('config')->where('name IN("img_small","img_medium","img_large")');
        return $this->_adapter->fetchAssoc($select);
    }

/**
     * Get all featured areas names
	 *
     * @return <array>
     */
    public function selectFeaturedAreasNames()
    {
        $select = $this->_adapter->select()->from('featured_area',array('name'=>'name'))->order("name ASC");
        $adapter = $this->getAdapter();
        return $adapter->fetchAssoc($select, null, Zend_Db::FETCH_ASSOC);
    }


	/**
     * Get all pages
	 *
     * @return <array>
     */
    public function selectAllPages()
    {
        $select = $this->_adapter->select()->from($this->_pageTableName);
        return $this->_adapter->fetchAll($select);
    }

    public function selectAllVariables()
    {
        $select = $this->_adapter->select()->from($this->_variableTableName, array('name'=>'name') );
        return $this->_adapter->fetchAll($select);
    }

     public function selectAdminEmail()
    {
        $select = $this->getAdapter()->select()->from('config')->where('name = ?', 'admin_email');
        return  $this->getAdapter()->fetchPairs($select);
    }

	public function insertThxPageCategory($parentCategoryId, $name) {
		$insertData = array(
			'id_parent' => $parentCategoryId,
			'name'      => $name
		);
		$this->getAdapter()->insert('category', $insertData);
        return $this->getAdapter()->lastInsertId('category');
	}

	public function selectTemplateIdByName($templateName) {
		$select = $this->getAdapter()->select()
			->from(
				array('t' => 'template'),
				'id'
			)
			->where('t.name=?', $templateName);
		return $this->getAdapter()->fetchOne($select);
	}
    
    public function insertConstantContact(array $constantContactData) {
		
		$this->_adapter->insert($this->_configTableName, $constantContactData);
		
	}
    
    public function selectConstantContactLogin() {
		$select = $this->getAdapter()->select()
			->from(
				$this->_configTableName,
				array(
					'value',
                )
			)
			->where('name=?', 'constantContactLogin');
		return $this->_adapter->fetchOne($select, null, Zend_Db::FETCH_OBJ);
	}
    
    public function selectConstantContactPassword() {
		$select = $this->getAdapter()->select()
			->from(
				$this->_configTableName,
				array(
					'value',
                )
			)
			->where('name=?', 'constantContactPassword');
		return $this->_adapter->fetchOne($select, null, Zend_Db::FETCH_OBJ);
	}
    
    public function selectConstantContactApikey() {
		$select = $this->getAdapter()->select()
			->from(
				$this->_configTableName,
				array(
					'value',
                )
			)
			->where('name=?', 'constantContactApikey');
		return $this->_adapter->fetchOne($select, null, Zend_Db::FETCH_OBJ);
	}
    
     public function deleteConstantContactLogin() {
        $login = 'constantContactLogin';   
        $where = 'name ="'.trim($login).'"';
        $this->getAdapter()->delete($this->_configTableName, $where);
            
     }
     public function deleteConstantContactPassword() {
        $password = 'constantContactPassword';   
        $where = 'name ="'.trim($password).'"';
        $this->getAdapter()->delete($this->_configTableName, $where);
            
     }
     public function deleteConstantContactApikey() {
        $apikey = 'constantContactApikey';   
        $where = 'name ="'.trim($apikey).'"';
        $this->getAdapter()->delete($this->_configTableName, $where);
            
     }
     public function insertConstantContactlist($constantContactData) {
        $this->_adapter->insert($this->_configTableName, $constantContactData);
            
     }
     public function deleteConstantContactList() {
        $list = 'constantContactList';   
        $where = 'name ="'.trim($list).'"';
        $this->getAdapter()->delete($this->_configTableName, $where);
            
     }
     public function selectConstantContactFlag($formId) {
		$select = $this->getAdapter()->select()
			->from(
				$this->_formsTableName,
				array(
					'useConstantContact',
                )
			)
			->where('id=?', $formId);
		return $this->_adapter->fetchOne($select, null, Zend_Db::FETCH_OBJ);
	}
     
    public function selectConstantContactList($formId) {
		$select = $this->getAdapter()->select()
			->from(
				$this->_formsTableName,
				array(
					'constantContactList',
                )
			)
			->where('id=?', $formId);
		return $this->_adapter->fetchOne($select, null, Zend_Db::FETCH_OBJ);
	}
    public function updateConstantContactList($constantContactList, $formId) {
		$formData = array(
                    'constantContactList' => $constantContactList
                    );
		$where = sprintf("id = %d", $formId);
                $this->_adapter->update($this->_formsTableName, $formData, $where);
		
	}
}